package com.itheima.reggie.controller;
import cn.hutool.core.io.IoUtil;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.PutObjectRequest;
import com.itheima.reggie.common.R;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
@RestController
@RequestMapping("/common")
public class UploadController {
    @Value("${reggie.path}")
    private String BasePath;

    //文件上传
    @PostMapping("/upload")
    public R Upload(MultipartFile file) throws FileNotFoundException {
        String originalFilename = file.getOriginalFilename();
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
        String s = UUID.randomUUID().toString();
        String Filename = s + substring;
        String endpoint = "https://oss-cn-nanjing.aliyuncs.com";
        String accessKeyId = "LTAI5tCdA99w1iAxfLp6CPHE";
        String accessKeySecret = "iDB3rkheT56ojqOvBmgzLvzJM8kHjn";
        String bucketName = "reggie-project-img";
        String filePath = "E:\\img\\" + originalFilename;
        InputStream inputStream = new FileInputStream(filePath);
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        try {

            // 创建PutObjectRequest对象。
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, Filename, inputStream);
            ossClient.putObject(putObjectRequest);
            Filename = "https://reggie-project-img.oss-cn-nanjing.aliyuncs.com/" + Filename;
        } catch (Exception oe) {
            System.out.println("Error Message:" + oe.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }

      /*  File File = new File(BasePath);
        if(!File.exists()){
            File.mkdirs();
        }
        try {
            file.transferTo(new File(BasePath,Filename));
        } catch (IOException e) {
            e.printStackTrace();
        }
*/
        return R.success(Filename);
        }



    //文件下载
    @GetMapping("/download")
    public void download(String name, HttpServletResponse response) throws IOException {
        FileInputStream fileInputStream1 = new FileInputStream(BasePath + name);
        IoUtil.copy(fileInputStream1,response.getOutputStream());
        fileInputStream1.close();
       /* String endpoint = "https://oss-cn-nanjing.aliyuncs.com";
        // 阿里云账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维，请登录RAM控制台创建RAM用户。
        String accessKeyId = "LTAI5tCdA99w1iAxfLp6CPHE";
        String accessKeySecret = "iDB3rkheT56ojqOvBmgzLvzJM8kHjn";
        // 填写Bucket名称，例如examplebucket。
        String bucketName = "reggie-project-img";
        String objectName =name;
        String pathName = "E:\\img\\"+name;
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        try {
            ossClient.getObject(new GetObjectRequest(bucketName, objectName), new File(pathName));
            FileInputStream fileInputStream1 = new FileInputStream(name);
            IoUtil.copy(fileInputStream1,response.getOutputStream());
            fileInputStream1.close();
        }
        catch (Exception ce) {
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }*/
    }
}